<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: customers_group.php 40 2013-09-21 11:55:43Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 40 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:55:43 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
*
*	Original by André Estel 2008
*
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

switch ($_GET['action']) {
    case 'send':
        if(isset($_POST['cg']) && is_array($_POST['cg'])) {
            if (isset($_POST['categories']) || isset($_POST['products']) || isset($_POST['content']) || isset($_POST['boxes'])) {
                if (isset($_POST['categories'])) {
                    foreach ($_POST['cg'] AS $cgID => $value)
                        $db->db_query("UPDATE ".TABLE_CATEGORIES." SET group_permission_".(int)$cgID." = '".($_POST['permission'] == 'true'?'1':'0')."'");

                    $message_stack->add(constant('TEXT_CATEGORIES_SUCCESSFULLY_'.($_POST['permission'] == 'true'?'SET':'UNSET')), 'success');
                }
                if (isset($_POST['products'])) {
                    foreach ($_POST['cg'] AS $cgID => $value)
                        $db->db_query("UPDATE ".TABLE_PRODUCTS." SET group_permission_".(int)$cgID." = '".($_POST['permission'] == 'true'?'1':'0')."'");

                    $message_stack->add(constant('TEXT_PRODUCTS_SUCCESSFULLY_'.($_POST['permission'] == 'true'?'SET':'UNSET')), 'success');
                }
                if(isset($_POST['boxes'])) {
                	$result = $db->db_query("SELECT id, group_ids FROM ".TABLE_BOXES." ORDER BY id");
                	while (!$result->EOF) {
                        $values = explode(',', $result->fields['group_ids']);
                        if (in_array('', $values))
                            unset($values[array_search('', $values)]);

                        if ($_POST['permission'] == 'true') {
                            foreach ($_POST['cg'] AS $cgID => $value)
                                if (!in_array('c_'.$cgID.'_g', $values))
                                    $values[] = 'c_'.$cgID.'_g';

                            $group_ids = implode(',', $values);
                            $db->db_query("UPDATE ".TABLE_BOXES." SET group_ids = '".$group_ids."' WHERE id = '".$result->fields['id']."'");
                        } else {
                            foreach ($_POST['cg'] as $cgID=>$value)
                                if (in_array('c_'.$cgID.'_g', $values))
                                    unset($values[array_search('c_'.$cgID.'_g', $values)]);

                            $group_ids = implode(',', $values);
                            $db->db_query("UPDATE ".TABLE_BOXES." SET group_ids = '".$group_ids."' WHERE id = '".$result->fields['id']."'");
                        }
                        $result->MoveNext();
                    }
                    $message_stack->add(constant('TEXT_BOXES_SUCCESSFULLY_'.($_POST['permission'] == 'true'?'SET':'UNSET')), 'success');
                }
                if (isset($_POST['content'])) {
                    $result = $db->db_query("SELECT content_id, group_ids FROM ".TABLE_CONTENT_MANAGER." ORDER BY content_id");
                    
                    while (!$result->EOF) {
                        $values = explode(',', $result->fields['group_ids']);
                        if (in_array('', $values))
                            unset($values[array_search('', $values)]);

                        if ($_POST['permission'] == 'true') {
                            foreach ($_POST['cg'] AS $cgID => $value)
                                if (!in_array('c_'.$cgID.'_group', $values))
                                    $values[] = 'c_'.$cgID.'_group';

                            $group_ids = implode(',', $values);
                            $db->db_query("UPDATE ".TABLE_CONTENT_MANAGER." SET group_ids= '".$group_ids."' WHERE content_id = '".$result->fields['content_id']."'");
                        } else {
                            foreach ($_POST['cg'] as $cgID=>$value)
                                if (in_array('c_'.$cgID.'_group', $values))
                                    unset($values[array_search('c_'.$cgID.'_group', $values)]);

                            $group_ids = implode(',', $values);
                            $db->db_query("UPDATE ".TABLE_CONTENT_MANAGER." SET group_ids= '".$group_ids."' WHERE content_id = '".$result->fields['content_id']."'");
                        }
                        $result->MoveNext();
                    }
                    $message_stack->add(constant('TEXT_CONTENT_SUCCESSFULLY_'.($_POST['permission'] == 'true'?'SET':'UNSET')), 'success');
                }
            } else
                $message_stack->add(ERROR_PLEASE_SELECT_SHOP_AREA);

        } else
            $message_stack->add(ERROR_PLEASE_SELECT_CUSTOMER_GROUP);

        break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable" cellspacing="0" cellpadding="0">
	    <tr>
	        <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
	            <?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	        </td>
	        <td  class="boxCenter" valign="top">
	        	<table border="0" width="100%" cellspacing="0" cellpadding="2">
				    <tr>
				        <td>
				            <table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
				                <tr>
				                    <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
				                </tr>
				            </table>
				        </td>
				    </tr>
				    <tr>
				        <td valign="top">
				        	<?php echo (GROUP_CHECK == 'false' ? GROUP_DESC : ''); ?>
				            <table border="0" width="100%" cellspacing="0" cellpadding="0">
				                <tr>
				                    <td class="main">
					                    <?php echo  draw_form('customers_group', 'customers_group.php', 'action=send');
						                    $group = $db->db_query("SELECT 
					                    								customers_status_id,
					                                                     customers_status_name
					                                                 FROM 
					                                                 	".TABLE_CUSTOMERS_STATUS."
					                                                 WHERE 
					                                                 	language_id = ".(int)$_SESSION['languages_id']."
					                                                 ORDER 
					                                                 	BY customers_status_id ASC");
						                                                 	
						                    while (!$group->EOF) {
						                        echo draw_checkbox_field('cg['.$group->fields['customers_status_id'].']', '1').' '.$group->fields['customers_status_name'].'<br />';
						                        $group->MoveNext();
						                    }
						                    echo '<br />';
						                    echo draw_checkbox_field('categories', '1').' '.TEXT_CATEGORIES.'<br />';
						                    echo draw_checkbox_field('products', '1').' '.TEXT_PRODUCTS.'<br />';
						                    echo draw_checkbox_field('boxes', '1').' '.TEXT_BOXES.'<br />';
						                    echo draw_checkbox_field('content', '1').' '.TEXT_CONTENT.'<br />';
						                    echo '<br /><br />';
						                    echo '<strong>'.TEXT_PERMISSION.':</strong><br />
						                    		'.draw_radio_field('permission', 'true', true).' '.TEXT_SET.'<br />
						                    		'.draw_radio_field('permission', 'false', false).' '.TEXT_UNSET.'<br /><br />';
						                    ?>
						                    <button type="submit" class="button"><?php echo TEXT_SEND ?></button>
					                    </form>
				                    </td>
				                </tr>
				            </table>
				        </td>
				    </tr>
				</table>
			</td>
		</tr>
	</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>